<?php
namespace app\volcengine\controller;
use think\Controller;
use think\Db;


class Base extends Controller
{
    public $login_user = [];
    protected $noCheckToken = [];

    public function __construct(){
        parent::__construct();
        // 验证token
        $action = $this->request->action();
        if (!empty($this->noCheckToken)) {
            $noCheckToken = array_map('strtolower',$this->noCheckToken);
        }else{
            $noCheckToken = [];
        }

        
        if ( !in_array($action, $noCheckToken) ) {
            $header = request()->header();
            $authorization = $header['authorization'];
            if (empty($authorization)) {
                exception('AUTH_INFO missing',1001);
            }
            $authorization = str_replace('Bearer ', '', $authorization);

            $token = Db::name('sk')->field('sk,utype,user_id,ram_id,status')->where('sk',$authorization)->find();
            if ( empty($token) || $token['status'] == 0 ) {
                exception('Authorization is expired',1004);
            }

            $balance = Db::name('user')->where('id',$token['user_id'])->value('volcengine_balance');
            if ($balance <= 0) {
                exception('Insufficient quota',1005);
            }
            
            $this->login_user = $token;
        }
    }

   

}
